package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping//新增地址
    public R<String> save(@RequestBody AddressBook addressBook){
        //关联当前登录用户
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info(addressBook.toString());
        //添加到持久层
        addressBookService.save(addressBook);
        return R.success("保存地址成功");
    }

    @PutMapping("/default")//修改默认地址
    public R<String> defaultAddress(@RequestBody AddressBook addressBook){
        //update address_book set is_default = 0 where user_id = ?
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.set(AddressBook::getIsDefault,0);
        qw.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        addressBookService.update(qw);
        //设置默认的地址
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功");
    }
    @GetMapping("/{id}")//根据ID查询地址
    public R<AddressBook> getAddress(@PathVariable Long id){
        AddressBook byId = addressBookService.getById(id);
        if (byId != null) {
            return R.success(byId);

        }else {
            return R.error("没有找到当前地址");
        }

    }
    @GetMapping("/default")//查询默认地址
    public R<AddressBook> checkDefaultAddress(){
        //select * from address_book where is_default = ? and user_id = ?;
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getIsDefault,1);
        qw.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        AddressBook byId = addressBookService.getOne(qw);
        if (byId != null) {
            return R.success(byId);

        }else {
            return R.error("没有当前对象");
        }

    }
    @GetMapping("/list")//查询指定用户的全部地址
    public R<List<AddressBook>> listR(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(addressBook.getUserId() != null,
                AddressBook::getUserId,addressBook.getUserId());

        return R.success(addressBookService.list(qw));

    }
    @DeleteMapping//删除地址
    public R deleted(@RequestParam List<Long> ids){
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.in(AddressBook::getUserId,ids);
        qw.eq(AddressBook::getIsDefault,1);
        int count = addressBookService.count(qw);
        if (count > 1) {
            throw new CustomException("默认状态:不可删除");
        }
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }
    @PutMapping
    public R update(@RequestBody AddressBook addressBook){
        log.info("地址修改:{}",addressBook.toString());
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }


}
